/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;
import javax.faces.convert.Converter;
import util.enumeration.AccessRight;
/**
 *
 * @author WenYong
 */
@FacesConverter(value = "accessRightsConverter", forClass = util.enumeration.AccessRight.class)
public class AccessRightsConverter implements Converter {
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {    

        if (value == null || value.length() == 0) {
            return null;
        }

        if(value.equals("ADD_TCUBE_USER")) return AccessRight.ADD_TCUBE_USER;
        else return AccessRight.UNLOCK_ACCOUNTS;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null) {
            return null;
        }

        if (value instanceof AccessRight) {
            AccessRight accessRights = (AccessRight) value;

            if(accessRights.equals(accessRights.ADD_TCUBE_USER)) return "ADD_TCUBE_USER";
            else return "UNLOCK_ACCOUNTS";
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.AccessRights");
        }
    }
}
